<script>
/**
 * 根据Value值自切换状态的按钮
 *    props: { value: bool, icon: array|string, label: array|string, type: array:string }
 */
import YmIconButton from './YmIconButton'
export default {
  functional: true,
  render (h, context) {
    const { props, data, listeners } = context

    const getSwitch = (key) => {
      // 未定义
      if (props[key] === undefined) return undefined
      // 可切换
      if (Array.isArray(props[key])) return props[key][props.value ? 1 : 0]
      // 单一值
      return props[key]
    }

    const icon = getSwitch('icon')
    const label = getSwitch('label')
    const type = getSwitch('type')

    return h(YmIconButton, {
      ...data,
      props: {
        ...props,
        label,
        icon,
        type
      },
      on: listeners
    }, context.children)
  }
}

</script>
